<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="活动标题" prop="activateTitile">
          <el-input
            v-model="queryParams.activateTitile"
            placeholder="请输入活动标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="活动类型" prop="contentColumn">
          <el-select
            v-model="queryParams.note1"
            placeholder="请选择活动类型"
            clearable
          >
            <el-option label="讲座" value="讲座"></el-option>
            <el-option label="培训" value="培训"></el-option>
            <el-option label="协会" value="协会"></el-option>
            <el-option label="团建" value="团建"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="applyStartTime">
          <el-date-picker clearable
                          v-model="queryParams.applyStartTime"
                          format="yyyy-MM-dd HH:mm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动持续时间" prop="note2">
          <el-input
            v-model="queryParams.note2"
            placeholder="请输入活动持续时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建活动用户昵称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入用户昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="报名截止时间" prop="applyEndTime">
          <el-date-picker clearable
                          v-model="queryParams.applyEndTime"
                          format="yyyy-MM-dd HH:mm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          placeholder="请选择报名截止时间">
          </el-date-picker>
        </el-form-item>
        <br/>
        <div style="text-align:right;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
        <br/>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['yzx:activity:activity:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['yzx:activity:activity:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['yzx:activity:activity:export']"
          >导出
          </el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="activateList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="ID" align="center" prop="id"/>
        <el-table-column label="活动标题" align="center" prop="activateTitile"/>
        <el-table-column label="活动类型" align="center" prop="note1"/>
        <el-table-column label="活动开始时间" align="center" prop="applyStartTime" width="180"/>
        <el-table-column label="活动持续时间" align="center" prop="note2"/>
        <el-table-column label="创建活动用户昵称" align="center" prop="nickName"/>
        <el-table-column label="报名截止时间" align="center" prop="applyEndTime" width="180"/>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['yzx:activity:activity:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['yzx:activity:activity:remove']"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handlePUpdate(scope.row)"
              v-hasPermi="['yzx:activity:activity:bm']"
            >报名
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-container>
          <el-main>
            <el-row>
              <el-col span="12">
                <el-form-item label="活动标题" prop="activateTitile">
                  <el-input v-model="form.activateTitile" placeholder="请输入活动标题"/>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="活动类型" prop="note1">
                  <el-select
                    v-model="form.note1"
                    placeholder="请选择活动类型"
                    clearable
                    style="width:100%;"
                  >
                    <el-option label="讲座" value="讲座"></el-option>
                    <el-option label="培训" value="培训"></el-option>
                    <el-option label="协会" value="协会"></el-option>
                    <el-option label="团建" value="团建"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="活动说明" prop="activateIllustrate">
                <editor v-model="form.activateIllustrate" :min-height="200" placeholder="请输入内容"
                        style="width:100%;"/>
              </el-form-item>
            </el-row>
<!--            <el-row>
              <el-form-item label="活动图示" prop="activatePhoto">
                <image-upload v-model="form.activatePhoto"/>
              </el-form-item>
            </el-row>-->
            <el-row>
              <el-col span="12">
                <el-form-item label="活动开始时间" prop="applyStartTime">
                  <el-date-picker clearable
                                  v-model="form.applyStartTime"
                                  format="yyyy-MM-dd HH:mm"
                                  type="datetime"
                                  style="width:100%;"
                                  value-format="yyyy-MM-dd HH:mm"
                                  placeholder="请选择报名开始时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="报名截止时间" prop="applyEndTime">
                  <el-date-picker clearable
                                  v-model="form.applyEndTime"
                                  format="yyyy-MM-dd HH:mm"
                                  type="datetime"
                                  style="width:100%;"
                                  value-format="yyyy-MM-dd HH:mm"
                                  placeholder="请选择报名截止时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="活动持续时间" prop="note2">
                  <el-input
                    v-model="form.note2"
                    placeholder="请输入活动持续时间"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
<!--              <el-col :span="12">
                <el-form-item label="通知范围" prop="note3">
                  <el-select v-model="form.note3" multiple placeholder="请选择"
                             style="width: 100%">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>-->
            </el-row>
            <!--              <el-form-item label="报名状态" prop="applyState">
                            <el-input v-model="form.applyState" placeholder="请输入报名状态" />
                          </el-form-item>-->
            <!--              <el-form-item label="备注" prop="note">
                            <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
                          </el-form-item>-->


          </el-main>
        </el-container>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style>
.ql-toolbar.ql-snow {
  text-align: left;
  border: 1px solid #ccc;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  padding: 8px;
  /* float: none; */
}

.el-header, .el-footer {
  background-color: #FFFFFF;
  color: #333;
  text-align: left;
  line-height: 60px;
  font-style: oblique;

}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #FFFFFF;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

/*对话框标题样式*/
.el-dialog__title {
  /*color: #1ab394;*/
  font-weight: bold;
  font-size: 25px;
}
</style>
<script>
import {
  listActivate,
  getActivate,
  delActivate,
  addActivate,
  updateActivate,
  updatePActivate1
} from "@/api/module/yzx/activity/activity";
import watermark from "watermark-dom";
import {listUser} from "@/api/system/user";


export default {
  name: "Activate",
  mounted() {
    watermark.load({watermark_txt: '  '});
  },
  data() {
    return {
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      activateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        nickName: null,
        activateTitile: null,
        activateIllustrate: null,
        activatePhoto: null,
        applyStartTime: null,
        applyEndTime: null,
        applyState: null,
        note: null,
        note1: null,
        note2: null,
        note3: undefined,
        note4: null,
        note5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

        activateTitile: [
          {required: true, message: "标题不能为空", trigger: "blur"},
        ],
        applyStartTime: [
          {required: true, message: "活动开始时间不能为空", trigger: "blur"},
        ],
        applyEndTime: [
          {required: true, message: "报名截止时间不能为空", trigger: "blur"},
        ],
        note1: [
          {required: true, message: "类型不能为空", trigger: "blur"},
        ],
        note2: [
          {required: true, message: "活动持续时间不能为空", trigger: "blur"},
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUser();
  },
  methods: {
    getUser() {

      listUser().then(response => {
        let data = response.rows;
        for (let i = 0; i < data.length; i++) {
          this.options.push({
            value: data[i].userName,
            label: data[i].nickName,
          })
        }
      });
    },
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listActivate(this.queryParams).then(response => {
        this.activateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        deptId: null,
        nickName: null,
        activateTitile: null,
        activateIllustrate: null,
        activatePhoto: null,
        applyStartTime: null,
        applyEndTime: null,
        applyState: null,
        note: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动信息";
      });
    },
    /** bm按钮操作 */
    handlePUpdate(row) {
      const id = row.id;
      getActivate(id).then(response => {
        this.form = response.data;
        updatePActivate1(this.form).then(response => {
          this.$modal.msgSuccess("报名成功");
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            //后台接收的数据是string类型，前台传值需要把数组类型的数据转换成string类型
            if (this.form.note3 != undefined) {
              this.form.note3 = this.form.note3.toString()
            }
            this.form.applyStartTime = this.form.applyStartTime + ":00";
            this.form.applyEndTime = this.form.applyEndTime + ":00";
            updateActivate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //后台接收的数据是string类型，前台传值需要把数组类型的数据转换成string类型
            if (this.form.note3 != undefined) {
              this.form.note3 = this.form.note3.toString()
            }
            this.form.applyStartTime = this.form.applyStartTime + ":00";
            this.form.applyEndTime = this.form.applyEndTime + ":00";
            addActivate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动信息编号为"' + ids + '"的数据项？').then(function () {
        return delActivate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yzx/activity/activity/export', {
        ...this.queryParams
      }, `activate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
